package Management;

import java.util.ArrayList;
import java.util.Scanner;
import java.util.HashMap;

public class Manager {
    private ArrayList<Satellite> db;                // 存储所有卫星
    private double lloc;                                      // 最低高度1.2
    private double dis;                                       // 最小间距
    private Scanner in = new Scanner(System.in);
 
    Manager() {
        lloc = 1.2;
        dis = 0.2;
        db = new ArrayList<Satellite>();
    }

    private boolean check(double k) { // 查看选择轨道是否符合要求
        boolean res = true;
        for(int i = 0; i < db.size(); i++) {       // 循环所有卫星，对比高度
            double distance = k-db.get(i).get_loc();
            if(distance < 0) distance *= -1;
            if(distance < dis) {
                res = false;
                break;
            }
        }
        return res;
    }

    // 打印所有卫星信息
    private void print_allsate() {       
        for (int i = 1; i <= 120; i++) System.out.printf("="); System.out.println();
		System.out.printf("%s%25s%15s%15s","COSPARID","卫星名称","轨道半径","是否可用");
        for(int i = 0; i < db.size(); i++) {
        	System.out.printf("\n%s%29s%19s%19s",db.get(i).get_id(),db.get(i).get_name(),db.get(i).get_loc(),db.get(i).get_status());
		}
        System.out.println(); for (int i = 1; i <= 120; i++) System.out.printf("="); System.out.println();
    }

    private void add_sate() {    // 添加一个卫星
        String cosparid,name;
        double loc;
        boolean f = true;
        do {
        	System.out.printf("\nCOSPARID（六位）: "); cosparid = in.next();      // 使id输入合法
                f = true;
                for(int i = 0; i < db.size(); i++) {
                    if(db.get(i).get_id().equals(cosparid)) {
                        System.out.println("该ID已经存在！");
                        f = false;
                    }
                }
        } while(cosparid.length() != 6 || f == false);
        System.out.printf("\n名称: "); name = in.next();
        System.out.printf("\n轨道半径: "); loc = in.nextDouble();
        if(check(loc)) {
            Satellite s = new Satellite(cosparid,name,loc);
            System.out.println("注册下列新卫星成功！");
            System.out.println(s);
            db.add(s);
        } else {
            System.out.println("创建失败，该轨道不符合要求！");
        }
    }

    // 删除卫星
    private void del_sate() {
        System.out.println("请输入要删除卫星的COSPARID或名称：");
        boolean f = false;
        String s = in.next();
        int id_loc = 0;
        for(int i = 0; i < db.size(); i++) {
            if(db.get(i).get_id().equals(s) || db.get(i).get_name().equals(s)) {
                id_loc = i;
                db.remove(id_loc);
                f = true;
                break;
            }
        }
        if(f) System.out.println("删除成功！");
        else System.out.println("删除失败！");
    }
    
    // 激活卫星
    private void open_sate() {
        System.out.println("请输入需要激活卫星的COSPARID或名称：");
        boolean f = false;
        String s = in.next();
        for(int i = 0; i < db.size(); i++) 
            if(db.get(i).get_id().equals(s) || db.get(i).get_name().equals(s)) {
                db.get(i).change_status();
                f = true;
            }
        if(f) System.out.println("激活成功！");
        else System.out.println("激活失败！");
    }

    // 关闭卫星
    private void close_sate() {
        System.out.println("请输入需要关闭卫星的COSPARID或名称：");
        boolean f = false;
        String s = in.next();
        for(int i = 0; i < db.size(); i++) 
            if(db.get(i).get_id().equals(s) || db.get(i).get_name().equals(s) ) {
                db.get(i).change_status();
                f = true;
            }
        if(f) System.out.println("关闭成功！");
        else System.out.println("关闭失败！");
    }

    // 打印失活卫星
    private void pclose_sate() {
        for (int i = 1; i <= 120; i++) System.out.printf("="); System.out.println();
        System.out.printf("%s%25s%15s%15s","COSPARID","卫星名称","轨道半径","是否可用");
		for(int i = 0; i < db.size(); i++) {
            if(db.get(i).get_status()) continue;
            System.out.printf("\n%s%29s%19s%19s",db.get(i).get_id(),db.get(i).get_name(),db.get(i).get_loc(),db.get(i).get_status());
        }
        System.out.println(); for (int i = 1; i <= 120; i++) System.out.printf("="); System.out.println();
    }

    // 模糊查找
    private void find_sate() {
		System.out.println("根据卫星名称模糊查询，请输入卫星名称：");
		String sub = in.next();
		for(int i = 0; i < 120; i++) System.out.printf("="); System.out.println();
		System.out.printf("%s%25s%15s%15s","COSPARID","卫星名称","轨道半径","是否可用");
		for(int i = 0; i < db.size(); i++) {
			if(db.get(i).get_name().indexOf(sub) == -1) continue;
			System.out.printf("\n%s%29s%19s%19s",db.get(i).get_id(),db.get(i).get_name(),db.get(i).get_loc(),db.get(i).get_status());
		}
		System.out.println(); for(int i = 0; i < 120; i++) System.out.printf("="); System.out.println();
    }


    // 修改卫星信息
    private void change_sate() {
        System.out.println("请输入需要修改的卫星的COSPARID或名称：");
        String s = in.next();
        for(int i = 0; i < db.size(); i++) {
            if(db.get(i).get_id().equals(s) || db.get(i).get_name().equals(s) ) {
                System.out.println("请输入卫星的新名称：");
                db.get(i).change_name(in.next());
                System.out.println("修改成功！");
                break;
            }
         }
    }

    private void quit() {
         System.exit(0);
    }

    private void alert() {
        System.out.println("不合法选择！");
    }

    public void execute(int k) {
        switch (k) {
            case 1:  print_allsate();  break;
            case 2:  add_sate();    break;
            case 3:  del_sate();    break;   // 删除旧卫星
            case 4:  open_sate();   break; // 激活卫星
            case 5:  close_sate();  break;  // 封锁卫星
            case 6:  pclose_sate(); break; // 显示失活卫星
            case 7:  find_sate();   break;   // 按名称模糊查找卫星
            case 8:  change_sate(); break; // 修改卫星信息
            case 9:  quit();        break;   // 退出
            default: alert();       break;   // 不合法
        }
    }
}
